<?php

/**
 * Erra_Utils_Image Image and gallery handling
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Erra_Utils_Image {

   var $image;
   var $image_type;

   public function __construct($filename) {
   		if(file_exists($filename))
			$this->load($filename);
   }
  /**
    * Erra_Utils_Image::load() Loads and image from a given filename
    *
    * @param mixed $filename
    * @return
    */
   function load($filename) {
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }

   /**
    * Erra_Utils_Image::save() Saves an image according to given parameters
    *
    * @param mixed $filename
    * @param mixed $image_type
    * @param integer $compression
    * @param mixed $permissions
    * @return
    */
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=95, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
  /**
    * Erra_Utils_Image::output()  outputs image
    *
    * @param mixed $image_type
    * @return
    */
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }
   }

 /**
    * Erra_Utils_Image::getWidth() Returns image width
    *
    * @return
    */
   function getWidth() {
      return imagesx($this->image);
   }

 /**
    * Erra_Utils_Image::getHeight() Returns image height;
    *
    * @return
    */
   function getHeight() {
      return imagesy($this->image);
   }

 /**
    * Erra_Utils_Image::resizeToHeight() Resizes image to given height
    *
    * @param mixed $height
    * @param mixed $upscale
    * @return
    */
   function resizeToHeight($height, $upscale=true) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      if($upscale == false && $this->getHeight() <= $height)
      		return;
      $this->resize($width,$height);
   }

 /**
    * Erra_Utils_Image::resizeToWidth()  Resizes image to given width
    *
    * @param mixed $width
    * @param mixed $upscale
    * @return
    */
   function resizeToWidth($width, $upscale=true) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
       if($upscale == false && $this->getWidth() <= $width)
      		return;
      $this->resize($width,$height);
   }

  /**
    * Erra_Utils_Image::scale()  Scales image to a given ratio
    *
    * @param mixed $scale
    * @return
    */
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }

  /**
    * Erra_Utils_Image::resize() Resizes image to a given width and height
    *
    * @param mixed $width
    * @param mixed $height
    * @return
    */
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }
     /**
    * Erra_Utils_Image::resizeCropped() Resizes image and crop it depending width
    *
    * @param mixed $width
    * @param mixed $height
    * @return
    */
   function resizeCropped($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      $croppedHeight = ($height/$width)*$this->getHeight();
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $croppedHeight);

      $this->image = $new_image;
   }
}
?>